# -*- coding:utf-8 -*-
'''
@author: xiaoming
@contact: lishihui0129@163.com
@time: 2017/6/29 17:04
@desc:
'''
import re
import os
import pickle
from demo.config import quhaoFile
from myUtil import telphonePattern,flagSplitPattern
from mysqlHelper import close,connectDB,insertSql

pkl_file = open(quhaoFile, 'rb')
quhaoInfo = pickle.load(pkl_file)
pkl_file.close()


def telphoneNumExtractList(string):
    res=re.findall(telphonePattern,string)

    temp=[]
    for each in res:
        tmp=each.replace('0086-','',1).replace('086-','',1).replace('86-','',1)
        temp.append(tmp.strip())

    return temp

def ExtractAreaCode(string):
    temp=[]
    item=string.strip()
    if len(item)==4:
        for key,value in quhaoInfo.items():
            if key in item and item in value:
                temp.append(item)
    return temp




def areaCodeProcess(string):
    '''
    :param string:
    :return:对同一行的电话号码进行区号不全
    '''
    res=telphoneNumExtractList(string)  #提取一行有多个号码的情况、号码跨行情况
    # 提取区号和号码跨行情况
    res1=ExtractAreaCode(string)

    res=res+res1
    return res

def telphoneNumExtract(string):
    '''
    :param string: 待提取对象
    :return:提取结果
    '''
    res=areaCodeProcess(string)

    temp=[]
    for item in res:
        linshi=item.strip()
        if len(linshi)==0:
            continue
        temp.append(linshi)
    return temp

def IncludeSwitchNum(string):
    if "，分机" in string:
        return string.replace("，分机","-").replace("—","-")
    else:
        return string.replace("—","-")

